package com.open.capacity.answer.service.impl;

import com.open.capacity.answer.dao.AnswerDao;
import com.open.capacity.answer.service.AnswerService;
import com.open.capacity.common.model.Answer;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerDao answerDao;

    @Override
    public PageResult<Answer> getSelectAnswers(Map<String, Object> params) {
        int count = answerDao.count(params);
        PageUtil.pageParamConver(params,true);
        List<Answer> answers = answerDao.getSelectAnswers(params);
        return PageResult.<Answer>builder().code(0).
                data(answers).count((long) count).build();
    }

    @Override
    public Result addAnswer(Answer answer) {
        int result = answerDao.addAnswer(answer);
        return Result.succeed(result == 1 ? "添加成功" : "添加失败");
    }

    @Override
    public Result updateAnswer(Answer answer) {
        int result = answerDao.updateAnswer(answer);
        return Result.succeed(result == 1 ? "修改成功" : "修改失败");
    }

    @Override
    public Result delAnswer(int id) {
        int result = answerDao.delAnswer(id);
        return Result.succeed(result > 0?"删除成功":"删除失败");
    }
}
